import { _decorator, animation, Animation, Camera, Component, easing, Node, RichText, tween, Vec2, Vec3,screen, view, AudioSource, AudioClip } from 'cc';
import ads from '../PlayableFramework/Ads/AdsManager';
import { UIManager } from '../PlayableFramework/Tools/Manager/UIManager';
import { Actor } from './Actor';
import { ResourceManager } from '../PlayableFramework/Tools/Manager/ResourceManager';
import { Button_3D } from '../PlayableFramework/Tools/Button_3D';
import { MoneyGen } from './MoneyGen';
import { Guest } from './Guest';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {

    @property(Actor)
    public actor:Actor;

    @property(MoneyGen)
    public moneyStack:MoneyGen;

    @property(Node)
    public areaList:Node[] = [];


    @property(RichText)
    public moneyTxt:RichText;
    public money:number = 0;

    @property(RichText)
    public peopleTxt:RichText;
    public people:number = 99;


    @property(Animation)
    public zhaji:Animation;

    protected onLoad(): void {
        ads.init();
    }

    start() {
        UIManager.instance.onScreenDirChanged = (dir)=>{
            console.log(dir);
        }
        // for (let i = 0; i < this.areaList.length; i++) {
        //     this.areaList[i].active = false;
        // }
        this.step1();

    }

    update(deltaTime: number) {
        this.moneyTxt.string = Math.ceil(this.money).toString();
        this.peopleTxt.string = this.people.toString()+"/99";

        if(this.areaIndex >=0 )
        {
            this.guide.node.active = true;
            const a = this.camera.convertToUINode(this.areaList[this.areaIndex].getChildByName("GuidePoint").worldPosition.clone(),this.guide.node.parent);
            this.guide.node.setPosition(a);
        }
        else{
            this.guide.node.active = false;
        }
    }

    baseClick(){
        this.audio.playOneShot(this.audioClips[0]);
    }

    public areaIndex:number = -1;
    @property(Camera)
    public camera:Camera;
    private step1(){
        this.areaList[0].active = true;
        this.areaList[0].getChildByName("button").active = true;
        this.areaIndex = 0;
    }

    //引导玩家收钱
    public click1(){
        this.baseClick();
        this.areaIndex = -1;
        this.areaList[0].getChildByName("button").active = false;
        console.log("开始移动去拿钱");
        this.actorMove("Path1",()=>{
            this.actor.node.eulerAngles = new Vec3(0,90,0);
            this.moneyStack.collect(this.actor.node, ()=>{
                this.actor.fillMoney();
                this.money+=2;
            });
            tween(this).delay(1).call(()=>{
                this.step2();
            }).start();
        });
    }

    private step2(){
        this.areaList[1].active = true;
        this.areaList[1].getChildByName("button").active = true;
        this.areaIndex = 1;
    }

    @property(AudioClip)
    public audioClips:AudioClip[] = [];
    @property(AudioSource)
    public audio:AudioSource;
    public taking:boolean = false;
    //引导建造房子
    public click2(){
        this.baseClick();
        this.areaIndex = -1;
        console.log("建立山地车商店拉");
        this.areaList[1].getChildByName("button").active = false;
        this.actorMove("Path2",()=>{
            const des = this.areaList[1].getChildByName("high").worldPosition;
            for (let i = 0; i < 100; i++) {
                const index = i;
                tween(this.actor.node).delay(index * 0.005).call(()=>{
                    const obj = ResourceManager.spawn("MoneyStack",this.areaList[1].getChildByName("pool"));
                    obj.node.worldPosition = this.actor.node.worldPosition;
                    tween(obj.node).delay(index * 0.005).to(0.2,{worldPosition:des},{easing:easing.sineOut}).to(0.2,{worldPosition:this.areaList[1].worldPosition},{easing:easing.sineIn}).call(()=>{
                        this.actor.costMoney();
                        ResourceManager.despawn(obj,null);
                        this.money -=1;

                        if(index == 0){
                            this.areaList[1].getComponent(Animation).play("ShopOpen");
                        }
                    }).start();
                }).start();
            }
            tween(this.actor.node).delay(1.5).call(()=>{
                this.areaList[1].getChildByName("model").active = true;
                this.areaList[1].getChildByName("ShopMask-001").active = false;
                this.audio.playOneShot(this.audioClips[1]);
                this.step3();
            }).start();
        });
    }

    public step3(){

        this.areaList[2].getComponent(Animation).play("BikeOpen");
        tween(this.actor.node).delay(1).call(()=>{
            this.areaList[2].getComponent(Animation).enabled = false;;
            this.areaList[2].active = true;
            this.areaList[2].getChildByName("button").active = true;
            this.areaList[2].getChildByName("model").active = true;
            this.areaIndex = 2;

        }).start();
    }

    //点击车堆
    public click3(){
        this.baseClick();
        this.areaIndex = -1;
        this.areaList[2].getChildByName("button").active = false;
        this.actorMove("Path3",()=>{
            this.actor.node.eulerAngles = new Vec3(0,0,0);
            this.taking = true;
            this.actor.anim.play("take_idle");
            for (let i = 0; i < 3; i++) {
                const bike = this.areaList[2].getChildByName("model").children[0];
                const index = i;
                const child = this.actor.node.getChildByName("BikeNode").children[i];
                bike.setParent(child,true);

                tween(bike).delay(index * 0.3).to(0.3,{worldPosition:child.worldPosition,worldRotation:child.worldRotation}).call(()=>{
                }).start();
            }

            tween(this.actor.node).delay(1.5).call(()=>{
                this.step4();
            }).start();
        });
    }

    public step4(){

        this.areaList[3].active = true;
        this.areaList[3].getChildByName("button").active = true;
        this.areaList[3].getChildByName("model").active = true;
        this.areaIndex = 3;


    }

    sortQueue(queue:Guest[]){
        for (let i = 0; i < queue.length; i++) {
            const index = i;
            tween(queue[i].node).to(0.2,{position:new Vec3(0,0,-3*i)}).start();
        }
    }

    //点击车车
    public click4(){
        this.baseClick();
        this.areaIndex = -1;
        const queue:Guest[]= [];
        var queueParent = this.areaList[3].getChildByName("queue");
        for (let i = 0; i < queueParent.children.length; i++) {
            queue.push(queueParent.children[i].getComponent(Guest));
        }

        this.areaList[3].getChildByName("button").active = false;
        this.actorMove("Path4",()=>{

            const pathRoot = ResourceManager.getPoolNode("Path4_1");
            const pathRoot2 = ResourceManager.getPoolNode("Path4_2");

            //骑手1
            {
                const biker = queue.shift();
                const bike = this.actor.node.getChildByName("BikeNode").children[0].children[0];

                tween(bike).delay(0).to(0.3,{worldPosition:biker.node.worldPosition}).call(()=>{
                    this.sortQueue(queue);

                    bike.active = false;
                    biker.ride();
    
                    biker.mover.setPathRoot(pathRoot);
                    biker.mover.move();
                    biker.anim.play("ride_bike")

                    //抬杆
                    this.zhaji.play("zhaji");
                    tween(this.actor.node).delay(0.1).call(()=>{
                        ResourceManager.spawn("MoneyEffect",this.zhaji.node);
                        this.people--;
                        this.moneyStack.gen(50,this.zhaji.node.worldPosition,()=>{
                            this.money = 100;
                        });
                    }).start();
                    
                    biker.mover.onPathComplete = ()=>{
                        //开始风火轮
                        {
                            const center1 = this.areaList[3].getChildByName("center1");
                            biker.node.setParent(center1,true);

                            const rotateTime = 1.5;
   
                            tween(center1).to(rotateTime,{eulerAngles:new Vec3(-360,-90,0)}).start();
                            tween(biker.node).to(rotateTime,{position:new Vec3(4,-6.116,0)}).call(()=>{
                                biker.mover.setPathRoot(pathRoot2);
                                biker.mover.move();
                                biker.mover.onPathComplete = () =>{
                                    console.log("减少一个人");
                                };
                            }).start();
                        }

                        //下一辆车开始
                        const biker2 = queue.shift();
                        const bike2 = this.actor.node.getChildByName("BikeNode").children[1].children[0];
                        tween(bike2).delay(0).to(0.3,{worldPosition:biker2.node.worldPosition}).call(()=>{
                            this.sortQueue(queue);

                            bike2.active = false;
                            biker2.ride();
            
                            biker2.mover.setPathRoot(pathRoot);
                            biker2.mover.move();
                            biker2.anim.play("ride_bike")

                            this.zhaji.play("zhaji");
                            tween(this.actor.node).delay(0.1).call(()=>{
                                ResourceManager.spawn("MoneyEffect",this.zhaji.node);
                                this.people--;
                                this.moneyStack.gen(100,this.zhaji.node.worldPosition,()=>{
                                    this.money = 200;
                                });
                            }).start();

                            biker2.mover.onPathComplete = ()=>{
                                //开始风火轮
                                {
                                    const center2 = this.areaList[3].getChildByName("center2");
                                    biker2.node.setParent(center2 , true);

                                    const rotateTime = 1.5;

                                    tween(center2).to(rotateTime,{eulerAngles:new Vec3(-360,-90,0)}).start();
                                    tween(biker2.node).to(rotateTime,{position:new Vec3(4,-6.116,0)}).call(()=>{
                                        biker2.mover.setPathRoot(pathRoot2);
                                        biker2.mover.move();
                                        biker2.mover.onPathComplete = () =>{
                                            console.log("减少一个人");
                                        };
                                    }).start();
                                }
        
                                //下一辆车开始
                                const biker3 = queue.shift();
                                const bike3 = this.actor.node.getChildByName("BikeNode").children[2].children[0];
                                this.actor.anim.play("idle")
                                tween(bike3).delay(0).to(0.3,{worldPosition:biker3.node.worldPosition}).call(()=>{
                                    this.sortQueue(queue);

                                    bike3.active = false;

                                    biker3.ride();
                                    biker3.mover.setPathRoot(pathRoot);
                                    biker3.mover.move();
                                    biker3.anim.play("ride_bike")

                                    this.zhaji.play("zhaji");
                                    tween(this.actor.node).delay(0.1).call(()=>{
                                        ResourceManager.spawn("MoneyEffect",this.zhaji.node);
                                        this.people--;
                                        this.moneyStack.gen(150,this.zhaji.node.worldPosition,()=>{
                                            this.money = 300;
                                        });
                                    }).start();

                                    biker3.mover.onPathComplete = ()=>{
                                        //开始风火轮
                                        {
                                            const center3 = this.areaList[3].getChildByName("center3");
                                            biker3.node.setParent(center3 , true);

                                            const rotateTime = 1.5;

                                            tween(center3).to(rotateTime,{eulerAngles:new Vec3(-360,-90,0)}).start();
                                            tween(biker3.node).to(rotateTime,{position:new Vec3(4,-6.116,0)}).call(()=>{
                                                biker3.mover.setPathRoot(pathRoot2);
                                                biker3.mover.move();
                                                biker3.mover.onPathComplete = () =>{

                                                };
                                            }).start();
                                        }
                                    };
                                }).start();
                            };
                        }).start();
                    };
                }).start();
            }
        });
        tween(this).delay(5).call(()=>{
            this.step5();
        }).start();
    }

    step5(){
        this.areaList[4].getChildByName("button").active = true;
        this.taking = false;
        this.areaIndex = 4;
    }

    //点击钱钱
    public click5(){
        this.baseClick();
        this.areaIndex = -1;
        this.areaList[4].getChildByName("button").active = false;
        this.actorMove("Path1",()=>{

            this.actor.node.eulerAngles = new Vec3(0,90,0);
            this.moneyStack.collect(this.actor.node, ()=>{
                this.actor.fillMoney();
            });
            tween(this).delay(1).call(()=>{
                this.step6();
            }).start();
        });
    }


    public onStep6:()=>void;
    step6(){
        this.areaList[5].getChildByName("button").active = true;
        this.areaList[5].getComponent(Animation).play("ConveyorMaskOpen");
        this.areaIndex = 5;
        this.onStep6();
    }
    
    //点击传送带了
    public click6(){
        this.baseClick();
        this.areaIndex = -1;
        this.areaList[5].getChildByName("button").active = false;
        const moneyEnd = this.areaList[5].getChildByName("moneyEnd");
        this.actorMove("Path6",()=>{
            this.actor.node.eulerAngles = new Vec3(0,90,0);

            for (let i = 0; i < this.actor.moneyProgress; i++) {
                const index = i;
                tween(this.actor.node).delay(index * 0.005).call(()=>{

                    //创建新钱
                    const obj = ResourceManager.spawn("MoneyStack",this.areaList[1].getChildByName("pool"));
                    obj.node.worldPosition = this.actor.node.getChildByName("Node").worldPosition;

                    const des = this.actor.node.getChildByName("Node").worldPosition.clone().add(new Vec3(0,2,0));


                    tween(obj.node).delay(index * 0.005).to(0.2,{worldPosition:des},{easing:easing.sineOut}).to(0.2,{worldPosition:moneyEnd.worldPosition},{easing:easing.sineIn}).call(()=>{
                        this.money-=3;
                        ResourceManager.despawn(obj,null);

                        if(index == 0){
                            this.areaList[5].getComponent(Animation).play("ConveyorOpen");
                        }

                        //花掉老钱
                        this.actor.costMoney();
                    }).start();


                }).start();
            }

            tween(this.actor.node).delay(2).call(()=>{
                this.audio.playOneShot(this.audioClips[1]);
                this.areaList[5].getComponent(Animation).play("ConveyorBike");
            }).start();

            tween(this.actor.node).delay(5).call(()=>{
                this.step7();
            }).start();
        });
    }

    @property(Animation)
    public endAnim:Animation
    step7(){
        this.endAnim.play("SceneExpansion");
        tween(this.camera.node).to(0.4,{position:new Vec3(-3,-2,-50)}).start();
        tween(this.actor.node).delay(2).call(()=>{
            this.areaList[6].getChildByName("button").active = true;
            this.areaIndex = 6;
        }).start();
    }

    //点击传送带了
    public click7(){
        this.baseClick();
        this.areaIndex = -1;
        this.areaList[6].getChildByName("button").active = false;
        console.log("建立自动传送带拉");
        ads.redirect();
        ads.end();
    }

    @property(Animation)
    public guide:Animation;

    actorMove(pathName:string,func = ()=>{}){
        console.log("开始走"+pathName);
        var pathRoot = ResourceManager.getPoolNode(pathName);
        this.actor.mover.setPathRoot(pathRoot);
        this.actor.mover.move();
        if(this.taking){
            this.actor.anim.play("take_run");
        }
        else{
            this.actor.anim.play("running")
        }
        this.actor.mover.onPathComplete = ()=>{
            if(this.taking){
                this.actor.anim.play("take_idle");
            }
            else{
                this.actor.anim.play("idle")
            }
            func();
        };
    }

}


